<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="账户名称" prop="accountName">
        <el-select v-model="queryParams.accountName" clearable placeholder="请选择" @keyup.enter.native="handleQuery">
          <el-option v-for="dict in dict.type.cld_account_name" :key="dict.value" :label="dict.label"
                     :value="parseInt(dict.value)"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input v-model="queryParams.companyName" placeholder="请输入公司名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="银行卡号" prop="bankCardNo">
        <el-input v-model="queryParams.bankCardNo" placeholder="请输入银行卡号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="收支类型" prop="operaType">
        <el-select v-model="queryParams.operaType" placeholder="请选择收支类型" clearable>
          <el-option v-for="item in operaType" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['cld:accountDetails:add']">新增账户流水</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="accountDetailsList" border>
      <el-table-column label="账户名称" align="center" prop="accountName" width="200px;"/>
      <el-table-column label="科目编号" align="center" prop="subjectNo" width="200px;"/>
      <el-table-column label="科目名称" align="center" prop="subjectName" width="200px;"/>
      <el-table-column label="项目名称" align="center" prop="projectName" width="200px;"/>
      <el-table-column label="收支类型" align="center" prop="operaType" width="100px;">
        <template slot-scope="scope">
          <span>{{ scope.row.operaType === 1 ? '收入' : '支出' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划支付时间" align="center" prop="planTime" width="150px;">
        <template slot-scope="scope">
          <span>{{ scope.row.planTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收支时间" align="center" prop="paymentTime" width="150px;">
        <template slot-scope="scope">
          <span>{{ scope.row.paymentTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应付金额" align="center" prop="answerAmount" width="150px;">
        <template slot-scope="scope">
          {{ toMoney(scope.row.answerAmount) }}
        </template>
      </el-table-column>
      <el-table-column label="实际金额" align="center" prop="realAmount" width="150px;">
        <template slot-scope="scope">
          {{ toMoney(scope.row.realAmount) }}
        </template>
      </el-table-column>
      <el-table-column label="支付人" align="center" prop="paymentName" width="120px;"/>
      <el-table-column label="摘要" align="center" prop="remark" />
      <el-table-column label="创建人名称" align="center" prop="createName" width="120px;"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160px;">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150px;">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['cld:accountDetails:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['cld:accountDetails:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改账户流水明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="收支类型" prop="operaType" >
          <el-radio-group v-model="form.operaType">
            <el-radio :label="1" border style="margin-right:5px;" >收入</el-radio>
            <el-radio :label="2" border >支出</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="项目名称" prop="projectId">
          <el-select filterable v-model="form.projectId" placeholder="请选择项目">
            <el-option v-for="item in projectOptions" :key="item.projectId" :label="item.projectName"
                       :value="item.projectId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支出科目" prop="subjectId2" v-if="form.operaType == 2">
          <el-cascader ref="cascader" v-model="form.subjectId2" :options="subjectOptions2" :props="{
                    expandTrigger: 'hover',
                    value: 'subjectId',
                    label: 'subjectName',
                    checkStrictly: true,
                  }" clearable @change="handleTypeChange" :show-all-levels="false" placeholder="请选择支出科目"></el-cascader>
        </el-form-item>
        <el-form-item label="收入科目" prop="subjectId1" v-if="form.operaType == 1">
          <el-cascader ref="cascader" v-model="form.subjectId1" :options="subjectOptions1" :props="{
                    expandTrigger: 'hover',
                    value: 'subjectId',
                    label: 'subjectName',
                    checkStrictly: true,
                  }" clearable @change="handleTypeChange" :show-all-levels="false" placeholder="请选择收入科目"></el-cascader>
        </el-form-item>
        <el-form-item label="计划时间" prop="planTime" v-if="form.operaType == 2">
          <el-date-picker clearable v-model="form.planTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择计划支付时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="收支时间" prop="paymentTime">
          <el-date-picker clearable v-model="form.paymentTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择收支时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="应付金额" prop="answerAmount" v-if="form.operaType == 2">
          <el-input type="number" v-model="form.answerAmount" placeholder="请输入应付金额" />
        </el-form-item>
        <el-form-item label="实际金额" prop="realAmount">
          <el-input type="number" v-model="form.realAmount" placeholder="请输入实际金额" />
        </el-form-item>
        <el-form-item label="支付人" prop="paymentName" v-if="form.operaType == 2">
          <el-input v-model="form.paymentName" placeholder="请输入支付人" />
        </el-form-item>
        <el-form-item label="摘要" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAccountDetails, getAccountDetails, delAccountDetails, addAccountDetails, updateAccountDetails } from "@/api/cld/accountDetails";
import {getList} from '@/api/cld/project'
import { listSubject, } from "@/api/cld/subject";

export default {
  name: "accountDetails",
  dicts: ['cld_subject_name'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 账户信息ID
      accountInfoId: null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 账户流水明细表格数据
      accountDetailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 30,
        accountName: null,
        companyName: null,
        projectName: null,
        backCardNo: null,
        subjectId: null,
        operaType: null,
      },
      // 表单参数
      form: {
        // 收支类型
        operaType:1
      },
      // 表单校验
      rules: {
        subjectId1: [
          { required: true, message: "请选择收入科目", trigger: "blur" },
        ],
        subjectId2: [
          { required: true, message: "请选择支出科目", trigger: "blur" },
        ],
        operaType: [
          { required: true, message: "请选择收支类型", trigger: "blur" },
        ],
      },
      // 收支类型
      operaType: [{
        value: '1',
        label: '收入'
      }, {
        value: '2',
        label: '支出'
      }],
      // 项目列表
      projectOptions: [],
      // 支出科目
      subjectOptions2: [],
      // 收入科目
      subjectOptions1: [],
    };
  },
  created() {
    const accountInfoId = this.$route.params && this.$route.params.accountInfoId;
    if (accountInfoId) {
      this.queryParams.accountInfoId = accountInfoId;
      this.getList();
      this.handleGetProjectList();
      this.getListSubject1();
      this.getListSubject2();
    }
  },
  methods: {
    /** 查询银行流水明细列表 */
    getList() {
      this.loading = false;
      listAccountDetails(this.queryParams).then(response => {
        this.accountDetailsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 获取科目列表 */
    getListSubject1() {
      listSubject({
        subjectType: 1
      }).then((response) => {
        this.subjectOptions1 = this.handleTree(
          response.data,
          "subjectId",
          "subjectPid"
        );
      });
    },
    /** 获取科目列表 */
    getListSubject2() {
      listSubject({
        subjectType: 2
      }).then((response) => {
        this.subjectOptions2 = this.handleTree(
          response.data,
          "subjectId",
          "subjectPid"
        );
      });
    },
    /** 科目选择 */
    handleTypeChange(e) {
      let nodesObj = this.$refs["cascader"].getCheckedNodes();
      if (nodesObj.length > 0) {
        let code = [];
        nodesObj[0].pathNodes.forEach((i) => {
          code.push(i.data.subjectNo);
        });
        // this.form.supplierTypeCode = code?.join(',');
      }
    },
    // 查询项目列表
    handleGetProjectList() {
      getList({
        projectStatus: 2
      }).then(res => {
        this.projectOptions = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        accountDetailsId: null,
        accountInfoId:this.queryParams.accountInfoId,
        paymentName: null,
        paymentTime: null,
        planTime: null,
        operaType: null,
        realAmount: null,
        answerAmount: null,
        remark: null,
        projectId: null,
        subjectId1: null,
        subjectId2: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.accountDetailsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加账户流水明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const accountDetailsId = row.accountDetailsId || this.ids
      getAccountDetails(accountDetailsId).then(response => {
        this.form = response.data;
        //科目组装字符串
        this.form.subjectId1 = this.form.subjectId1?.split(",").map(Number);
        this.form.subjectId2 = this.form.subjectId2?.split(",").map(Number);
        this.open = true;
        this.title = "修改账户流水明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          //科目组装字符串
          this.form.subjectId1 = this.form.subjectId1?.join(",");
          this.form.subjectId2 = this.form.subjectId2?.join(",");
          if (this.form.accountDetailsId != null) {
            updateAccountDetails(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAccountDetails(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const accountDetailsIds = row.accountDetailsId || this.ids;
      this.$modal.confirm('是否确认删除账户流水明细编号为"' + accountDetailsIds + '"的数据项？').then(function() {
        return delAccountDetails(accountDetailsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cld/accountDetails/export', {
        ...this.queryParams
      }, `details_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
